﻿using System.Collections.Generic;
using System.Data;
using MaLu.Data;
using System;

namespace MaLu.BusiLogic
{
    public class BLVisite
    {
        public static void SaveBLVisite(IDbProvider provider, Guid pid, string Leader,string Interviewee,DateTime ArrangeStartTime,
           DateTime ArrangeEndTime,string Contact,string Remark,string Creator, Guid deptid, string deptname, string deptdspname)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();
           paraList.Add(SqlHelper.Instance.CreateParameter("@VisiteID", DataType.GUID, pid));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Leader", DataType.VarChar, 50, Leader));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Interviewee", DataType.VarChar, 50, Interviewee));
           paraList.Add(SqlHelper.Instance.CreateParameter("@ArrangeStartTime", DataType.DateTime, ArrangeStartTime));
           paraList.Add(SqlHelper.Instance.CreateParameter("@ArrangeEndTime", DataType.DateTime, ArrangeEndTime));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Contact", DataType.VarChar, 50, Contact));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Remark", DataType.VarChar, 1000, Remark));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Creator", DataType.VarChar, 50, Creator));
           paraList.Add(SqlHelper.Instance.CreateParameter("@DepartmentID", DataType.GUID, deptid));
           paraList.Add(SqlHelper.Instance.CreateParameter("@DepartmentName", DataType.VarChar, 50, deptname));
           paraList.Add(SqlHelper.Instance.CreateParameter("@DepartmentDisplayName", DataType.VarChar, 50, deptdspname));

           SqlHelper.Instance.ExecuteProcedure(provider, "Proc_PSaveVisite", paraList.ToArray());
          
       }
       public static int DeleteVisite(IDbProvider provider, Guid pid)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();
           paraList.Add(SqlHelper.Instance.CreateParameter("@VisiteID", DataType.GUID, pid));

           int result = 0;
           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_PDeleteVisite", paraList.ToArray());
           int.TryParse(ds.Tables[0].Rows[0][0].ToString(), out result);
           return result;
       }

       public static string GetTemplate(IDbProvider provider,string pagename)
       {
           string template = string.Empty;
           DataTable ds = SqlHelper.Instance.ExecuteDataTable(provider, "select DataTemplate from VisiteFormItem where PageName='" + pagename + "'");
           if (ds.Rows.Count > 0)
           {
               if (ds.Rows[0][0] != null)
                   template = ds.Rows[0][0].ToString();
           }
           return template;
       }
       public static string GetVisiteItem(IDbProvider provider, Guid pid, string pagename, ref string projectname)
       {
           string template = string.Empty;
           List<IDataParameter> paraList = new List<IDataParameter>();
           paraList.Add(SqlHelper.Instance.CreateParameter("@PageName", DataType.VarChar, 200, pagename));
           paraList.Add(SqlHelper.Instance.CreateParameter("@VisiteID", DataType.GUID, pid));

           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_VGetVisiteContent", paraList.ToArray());
           if (ds.Tables[0].Rows.Count > 0)
           {
               template = ds.Tables[0].Rows[0][0].ToString();
           }
           if (ds.Tables[1].Rows.Count > 0)
           {
               projectname = ds.Tables[1].Rows[0][0].ToString();
           }


           return template;
       }
       public static void SaveVisiteItem(IDbProvider provider, Guid vid, string pagename, string content)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();          
           paraList.Add(SqlHelper.Instance.CreateParameter("@PageName", DataType.VarChar, 200, pagename));
           paraList.Add(SqlHelper.Instance.CreateParameter("@VisiteID", DataType.GUID, vid));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Content", DataType.Text, content));

           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_VSaveVisiteItem", paraList.ToArray());
         
       }

       public static DataTable GetVisiteDetailList(IDbProvider provider, int Year, int Month)
       {
           List<IDataParameter> paraList = new List<IDataParameter>();

           paraList.Add(SqlHelper.Instance.CreateParameter("@Year", DataType.Int32, 8, Year));
           paraList.Add(SqlHelper.Instance.CreateParameter("@Month", DataType.Int32, 8, Month));
           DataSet ds = SqlHelper.Instance.ExecuteProcedure(provider, "Proc_GetVisiteDetailList", paraList.ToArray());

           return ds.Tables[0];


       }
    } 
}
